﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Adapter.SISCONVE.Helper;
using Adapter.SISCONVE.Pages;
using AdapterSISCONVE.Helper;
using Business.ValueObject.SISCONVE;

namespace Adapter.SISCONVE.Components
{
    public class Consultas : Suite
    {
        private ConsultasPage _consultasPage;

        public Consultas()
            :base()
        {
            _consultasPage = new ConsultasPage(Manager);
        }

        public List<VOStatusDetailVehicle> GetStatusDetailCars()
        {
            Utilities.DeleteFileDownload(Constant.Consultas.FileName.EstadoReducidoMoviles);

            List<VOStatusDetailVehicle> result = new List<VOStatusDetailVehicle>();

            Thread.Sleep(2500);
            _consultasPage.ClickButtonConsultas();
            _consultasPage.ClickButtonSeguimientoMovil();
            _consultasPage.ClickButtonAgregarTodosVehiculos();
            Thread.Sleep(2500);
            _consultasPage.ClickButtonGenerarReporte();
            Thread.Sleep(2500);
            _consultasPage.ClickButtonExcel();
            Thread.Sleep(2500);
            _consultasPage.ClickLinkDescarga();
            Thread.Sleep(2500);

            try
            {
                DataTable table = ReadExcelFile(Constant.Consultas.FileName.EstadoReducidoMoviles);

                foreach (DataRow row in table.Rows)
                {
                    VOStatusDetailVehicle detailCar = new VOStatusDetailVehicle
                    {
                        MIId = row["MovilId"].ToString(),
                        Id = row["Matrícula"].ToString(),
                        CardId = row["CardId"].ToString(),
                        Brand = row["Marca"].ToString(),
                        Model = row["Modelo"].ToString(),
                        LastData = DateTime.Parse(row["Último_dato"].ToString()),
                        ReceptionLastData = DateTime.Parse(row["Recepción_último_dato"].ToString()),
                        LastDataWithInformation = DateTime.Parse(row["Último_dato_con_información"].ToString()),
                        ReceptionLastDataWithInformation = DateTime.Parse(row["Recepción_último_dato_con_información"].ToString()),
                        Speed = int.Parse(row["Velocidad"].ToString()),
                        SignalGPS = row["Señal_GPS"].ToString(),
                        SignalGPSR = Mapper.MapSignalGPSRToSignalGPSREnum(row["Señal_GPRS"].ToString()),
                        BatteryConnection = Mapper.MapBatteryConnectionToBatteryConnectionEnum(row["Conexión_Batería"].ToString()),
                        PositionACC = Mapper.MapPositionACCToPositionACCEnum(row["Posición_ACC"].ToString()),
                        KeyByPass = Mapper.MapKeyByPassToKeyByPassEnum(row["LlaveBayPass"].ToString())
                    };
                    result.Add(detailCar);
                }
            }
            catch (Exception ex)
            {
                result = null;
            }

            this.CleanUp();

            return result;
        }
    }
}
